import { useUserStore } from '../stores/user';

/**
 * 权限指令
 * 用法：v-permission="'system:user:add'"
 * 或者：v-permission="['system:user:add', 'system:user:edit']"
 */
export default {
  mounted(el, binding) {
    const userStore = useUserStore();
    const permissions = userStore.permissions;
    
    // 获取指令的值，可以是字符串或数组
    const value = binding.value;
    let hasPermission = false;
    
    if (typeof value === 'string') {
      // 单个权限验证
      hasPermission = permissions.includes(value);
    } else if (Array.isArray(value)) {
      // 多个权限验证，只要有一个权限匹配即可
      hasPermission = value.some(permission => permissions.includes(permission));
    }
    
    // 如果没有权限，则移除元素
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  }
};